package com.lvandroid.play.player;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.lvandroid.play.PlayerApplication;
import com.lvandroid.play.bean.ImageItem;
import com.lvandroid.play.util.LogUtil;
import com.lvandroid.player.R;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片播放器，支持单张/多张轮播
 */
public class ImagePlayer extends AllRoundPlayer<ViewGroup, ImageItem> {

    private List<String> mDataSources = new ArrayList<>();
    private XBanner.XBannerAdapter mDisplayAdapter;
    private LinearLayout mParentPanel;
    private XBanner mImageBanner;

    ImagePlayer() {
        mPlayItem = new ImageItem();
    }

    /**
     * 内部显示初始化
     */
    private void initDisplayInternal() {
        if(null == mImageBanner) {
            mParentPanel = (LinearLayout) PlayerApplication.getLayoutInflater().inflate(R.layout.layout_banner, null);
            mImageBanner = mParentPanel.findViewById(R.id.xbanner);
        }

        mDisplay.removeAllViews();
        mDisplay.addView(mParentPanel);
        initXBanner();
    }

    /**
     * 初始化x-banner控件
     */
    private void initXBanner() {
        if(null == mDisplayAdapter) {
            mDisplayAdapter = (banner, model, view, position) -> {
                LogUtil.i("test", "initXBanner : " + model);
                ImageView img = view.findViewById(R.id.img);
                //todo 默认的图片质量/大小，后续通过注入option的方式来动态修改
                Glide.with(PlayerApplication.getInstance().getGlobalContext())
                        .load(((ImageItem.ImageInfo)model).getXBannerUrl()).
                        into(img);
            };
        }
        mImageBanner.setBannerData(R.layout.layout_banner_item, mPlayItem.createImageInfo());
        mImageBanner.loadImage(mDisplayAdapter);
    }

    @Override
    public void start(List<String> source, ViewGroup display) {
        if(null == source || source.size() < 1 || null == display) {
            throw new IllegalArgumentException("IllegalArgument -> when call start : " +
                    "source = " + source + ", display = " + display);
        }

        setDataSource(source);
        setDisplay(display);
        start();
    }

    @Override
    public void setDataSource(List<String> source) {
        mDataSources = source;
        mPlayItem.setImageSources(source);
    }

    @Override
    public void setDisplay(ViewGroup v) {
        mDisplay = v;
        initDisplayInternal();
    }

    @Override
    public void start() {
        if(null != mImageBanner) {
            mImageBanner.startAutoPlay();
        }
    }

    @Override
    public void pause() {
        if(null != mImageBanner) {
            mImageBanner.stopAutoPlay();
        }
    }

    @Override
    public void resume() {
        if(null != mImageBanner) {
            mImageBanner.startAutoPlay();
        }
    }

    @Override
    public void stop() {
        if(null != mImageBanner) {
            mImageBanner.stopAutoPlay();
        }
    }

    @Override
    public void reset() {
        mDisplayAdapter = null;
        mImageBanner.stopAutoPlay();
        mImageBanner = null;
    }
}
